<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/5
 * Time: 15:04
 */

use core\Db;
use lib\Redis;


function db($name){
    return  Db::connect()->table($name);
}

/**
 * @param array $config
 * @param $p
 * @return bool
 * 启用redis
 */
function redis($config=[],$p=ture){
    return Redis::instance()->connect();
}



function BubbleSort($arr){
    $length = count($arr);
    if($length<=1){
        return $arr;
    }
    for($i=0;$i<$length;$i++){
        for($j=$length-1;$j>$i;$j--){
            if($arr[$j]<$arr[$j-1]){
                $tmp = $arr[$j];
                $arr[$j] = $arr[$j-1];
                $arr[$j-1] = $tmp;
            }
        }
    }
    return $arr;
}

function QSort($arr){
    p($arr);
    $length = count($arr);
    if($length <=1){
        echo '////<br />';
        return $arr;
    }
    $pivot = $arr[0];//枢轴
    $left_arr = array();
    $right_arr = array();
    for($i=1;$i<$length;$i++){//注意$i从1开始0是枢轴
        if($arr[$i]<=$pivot){
            $left_arr[] = $arr[$i];
        }else{
            $right_arr[] = $arr[$i];
        }
    }
    $left_arr = QSort($left_arr);//递归排序左半部分
    $right_arr = QSort($right_arr);//递归排序右半部份

  
    $a= array_merge($left_arr,array($pivot),$right_arr);//合并左半部分、枢轴、右半部分
    echo join(' ',$left_arr).'['.$pivot.']'.join(' ',$right_arr).'<br />';

    return $a;
}

//分页快捷方法
function page($page,$count, $size=20,$type=5){
    if($count<0 || $size<=0){
        return false;
    }
    $totalPage=ceil($count/$size);
    if($page<=1){
        $page=1;
    }
    if($page>=$totalPage){
        $page=$totalPage;
    }
    in_array($type,[3,5,7,9,11]) || $type=5;
    $pageAr=[];

    $pagen=($type-1)/2;
    $minPage=$page-$pagen;
    $maxPage=$page+$pagen;
    if($minPage<1){
        $minPage=1;
        $maxPage=$minPage+($type-1);
        $maxPage=($maxPage<=$totalPage)?:$totalPage;
    }
    if($maxPage>$totalPage){
        $maxPage=$totalPage;
        $minPage=$maxPage-($type-1);
        $minPage=($minPage>0)?:1;
    }
    for($minPage;$minPage<=$maxPage;$minPage++){
        $pageAr['list'][]=$minPage;
    }

    $pageAr['upPage']=$page-1;
    $pageAr['nextPage']=$page+1;
    if($pageAr['upPage']<1){
        $pageAr['upPage']=1;
    }
    if($pageAr['nextPage']>$totalPage){
        $pageAr['nextPage']=$totalPage;
    }
    $pageAr['page']=$page;
    $pageAr['totalPage']=$totalPage;
    $pageAr['totalRow']=$count;
    return $pageAr;
}

function get_url(){
    $http_type=http_type();
    $server_name=$_SERVER['HTTP_HOST'];
    $script_name=$_SERVER['SCRIPT_NAME']?:'';
    $path_info=$_SERVER['PATH_INFO']?:'';
    $query_str=$_SERVER['QUERY_STRING']?'?'.urldecode($_SERVER['QUERY_STRING']):'';
    $url=$http_type.$server_name.$script_name.$path_info.$query_str;
    return $url;
}

function http_type(){
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

return $http_type;
}




